package com.yun.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yun.unit.Code;
import com.yun.unit.R;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Component
public class QuanXianHanler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        System.out.println("当走到这里的时候，说明用户的权限不够");
        // 告知我们给前端响应的是JSON格式，编码格式是UTF-8
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        R r = new R(Code.Access_FAIL,"权限不足");
        //将这个对象转换成json字符串
        ObjectMapper o = new ObjectMapper();
        String s = o.writeValueAsString(r);
        //响应给前端
        httpServletResponse.getWriter().write(s);
        //打印异常
        e.printStackTrace();
    }
}
